$(function () {
  $("#jqGrid").Grid({
    url: '../collect/list',
    colModel: [
      {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
      {label: '用户', name: 'userName', index: 'user_name', width: 80},
      {label: '商品', name: 'valueName', index: 'value_name', width: 80},
      {label: '添加时间', name: 'addTime', index: 'add_time', width: 80}
      // {label: '提醒', name: 'isAttention', index: 'is_attention', width: 80}
      // {label: '类型', name: 'typeId', index: 'type_id', width: 80}
    ]
  });
});

var vm = new Vue({
  el: '#rrapp',
  data: {
    showList: true,
    title: null,
    collect: {},
    q: {
      name: ''
    }
  },
  methods: {
    query: function () {
      vm.reload();
    },
    add: function () {
      vm.showList = false;
      vm.title = "新增";
      vm.collect = {};
    },
    update: function (event) {
      var id = getSelectedRow("#jqGrid");
      if (id == null) {
        return;
      }
      vm.showList = false;
      vm.title = "修改";

      vm.getInfo(id)
    },
    saveOrUpdate: function (event) {
      var url = vm.collect.id == null ? "../collect/save" : "../collect/update";

      Ajax.request({
        type: "POST",
        url: url,
        contentType: "application/json",
        params: JSON.stringify(vm.collect),
        successCallback: function (r) {
          alert('操作成功', function (index) {
            vm.reload();
          });
        }
      });
    },
    del: function (event) {
      var ids = getSelectedRows("#jqGrid");
      if (ids == null) {
        return;
      }

      confirm('确定要删除选中的记录？', function () {

        Ajax.request({
          type: "POST",
          url: "../collect/delete",
          contentType: "application/json",
          params: JSON.stringify(ids),
          successCallback: function (r) {
            alert('操作成功', function (index) {
              vm.reload();
            });
          }
        });
      });
    },
    getInfo: function (id) {
      Ajax.request({
        url: "../collect/info/" + id,
        async: true,
        successCallback: function (r) {
          vm.collect = r.collect;
        }
      });
    },
    reload: function (event) {
      vm.showList = true;
      var page = $("#jqGrid").jqGrid('getGridParam', 'page');
      $("#jqGrid").jqGrid('setGridParam', {
        postData: {'name': vm.q.name},
        page: page
      }).trigger("reloadGrid");
    }
  }
});